/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.DetalleAdministrativo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Administrador
 */
public class AdminDetalleAdministrativo implements DetalleAdministrativoManager{
    private Connection c;
    public AdminDetalleAdministrativo(Connection c){
        this.c = c;
    }

    public void saveDetalleAdministrativo(DetalleAdministrativo da) throws DataException {
        PreparedStatement preSt = null;
        String query;
        try{
            query="INSERT INTO DETALLE_ADMINISTRATIVO VALUES("+da.getIdDetalleAdminist();
            query+= ", "+da.getIdFacultad();
            query+= ", "+da.getIdPais();
            query+= ", "+da.getIdCiudad();
            query+= ", "+da.getIdCargo()+")";            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException("AdminDetalleAdministrativo -> saveDetalleAdministrativo(DetalleAdministrativo da) "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    @Override
    public void updateDetalleAdministrativo(DetalleAdministrativo da) throws DataException {
        PreparedStatement preSt = null;
        try{
            String query = "UPDATE DETALLE_ADMINISTRATIVO SET IDFACULTAD = "+da.getIdFacultad();
            query+= ", IDPAIS = "+da.getIdPais();
            query+= ", IDCIUDAD = "+da.getIdCiudad();
            query+= ", IDCARGO = "+da.getIdCargo();
            query+= " WHERE IDDETALLE_ADMINISTRATIVO="+da.getIdDetalleAdminist();            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException(" AdminDetalleAdministrativo -> updateDetalleAdministrativo(DetalleAdministrativo da) e = "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
    @Override
    public DetalleAdministrativo getDetalleAdministBasic(int idUsuario) throws DataException{
        DetalleAdministrativo da = new DetalleAdministrativo();
        ResultSet rs = null;
        PreparedStatement preSt = null;
        try{
            String query = "SELECT IDFACULTAD, IDPAIS, IDCIUDAD, IDCARGO";
            query += " FROM DETALLE_ADMINISTRATIVO";
            query += " WHERE IDDETALLE_ADMINISTRATIVO="+idUsuario;            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            if(rs.next()){
                da.setIdDetalleAdminist(idUsuario);
                da.setIdFacultad(rs.getInt("IDFACULTAD"));
                da.setIdPais(rs.getInt("IDPAIS"));
                da.setIdCiudad(rs.getInt("IDCIUDAD"));
                da.setIdCargo(rs.getInt("IDCARGO"));
            }
        }catch(SQLException e){
            throw new DataException(" AdminDetalleAdministrativo -> getDetalleAdministBasic() e = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return da;
    }
}
